<?php session_start();
if(!isset($_SESSION['login']))
{
    $_SESSION['login'] = false;
}

    $_SESSION['page'] = 'register';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Register</title>

    <link rel="stylesheet" type="text/css" title="T1" href="stylesheet/progin.css" id="link_theme" />
	<link rel="stylesheet" type="text/css" title="T1" href="stylesheet/posting.css" />
		<meta name="description" content="RERGag, Make You Laughing" />
		<meta name="keywords" content="laugh" />
		<meta name="author" content="Robertus Theodore" />
		<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
<?php include 'header.php';?>
        <div class="main_container">
            <div id="preview"></div>
	
	<div align="left" style="width: 500px;" >
	<form id="posting_form" name="posting_form" method="post" action="register.php" onsubmit="checkOnSubmit()" enctype="multipart/form-data">
            <fieldset><legend>Posting Baru</legend>

                    <label class="label_posting">username </label><input id="form_nama" class="input_posting" name="username" type="text"></input><br/>

                    <label class="label_posting">password</label><input class="input_posting" id="password" type="password" name="password"></input><br />
                    <label class="label_posting">confirm password</label><input class="input_posting" id="cpassword" type="password" name="cpassword"></input><br />
                    
                    <label class="label_posting">e-mail</label><input class="input_posting" id="email" type="text" name="email"></input><br />
                    <label class="label_posting">confirm e-mail</label><input class="input_posting" id="cemail" type="text" name="cemail"></input><br />

                    <label class="label_posting">Tanggal Lahir</label><input class="input_posting" id="birthdate" type="text" name="birthdate"></input><br />
                    
                    <label class="label_posting">About Me</label><textarea class="textarea_posting" id="about_me" name="about_me" rows="5" cols="25"></textarea><br />
                    
                    <label class="label_posting">Gender </label><input id="option_link" class="input_posting" type="radio" name="gender" value="male"/>Male<br />
                    <label class="label_posting"></label><input id="option_image" class="input_posting" type="radio" name="gender" value="female"/>Female<br />
                    
                    <label class="label_posting">Upload Your Avatar </label><input class="input_posting" name="avatar" type="file" /><br />

                    <label class="label_posting">&nbsp;</label><input type="Submit" class="input_posting" value="Register"/>

            </fieldset>
        </form>
        <?php
            $con = mysql_connect("localhost","root","");
            if (!$con)
            {
                die('Could not connect: ' . mysql_error());
            }

            mysql_select_db("progin_171_13509008", $con);

            // get next id_content for image naming
//            global $curIDContent;
//            $qCount = mysql_query("SELECT MAX(id_content) FROM content");
//            $results = mysql_fetch_array($qCount);
//            $curIDContent = $results['MAX(id_content)'] + 1;

            //get form value
            $username = $_POST['username'];
            $password = $_POST['password'];
            $email = $_POST['email'];
            $birthdate = $_POST['birthdate'];
            if(isset($_POST['about_me'])){
                $about = $_POST['about_me'];
            }
            $gender = $_POST['gender'];
            //cek username
            $checkUsername = true;
            $queryCheckUsername = "SELECT * FROM user where user_name='".$username."'";
            $resultCheckUsername = mysql_query($queryCheckUsername);
            $rowCheckUsername = mysql_num_rows($resultCheckUsername);
            if($rowCheckUsername == 0){
                $checkUsername = true;
            }else{
                $checkUsername = false;
            }
            //cek email
            $checkEmail = true;
            $queryCheckEmail = "SELECT * FROM user where email='".$email."'";
            $resultCheckEmail = mysql_query($queryCheckEmail);
            $rowCheckEmail = mysql_num_rows($resultCheckEmail);
            if($rowCheckEmail == 0){
                $checkEmail = true;
            }else{
                $checkEmail = false;
            }
            //cek tanggal
            $checkDate = true;
            $dateSeparated = explode("-",$birthdate);
            echo $dateSeparated[0];
            echo $dateSeparated[1];
            echo $dateSeparated[2];
            if($dateSeparated[1] == 2){
                if(($dateSeparated[0]%4 == 0 && $dateSeparated[0]%100 != 0 )|| $dateSeparated[0]%400 == 0){
                    //cek tahun kabisat
                    if($dateSeparated[2] > 0 && $dateSeparated[2] <= 29){
                        $checkDate = true;
                    }else{
                        $checkDate = false;
                    }
                }else{
                    if($dateSeparated[2] > 0 && $dateSeparated[2] <= 28){
                        $checkDate = true;
                    }else{
                        $checkDate = false;
                    }
                }
            }else{
                if(($dateSeparated[1]%2 != 0 && $dateSeparated[1] > 0 && $dateSeparated[1] <= 7)||($dateSeparated[1]%2 == 0 && $dateSeparated[1] > 7 && $dateSeparated[1] <=12)){
                    //cek apakah bulan tersebut Januari, Maret, Mei, Juli, Agustus, Oktober, Desember
                    if($dateSeparated[2] > 0 && $dateSeparated[2] <= 31){
                        $checkDate = true;
                    }else{
                        $checkDate = false;
                    }
                }else{
                    if(($dateSeparated[1]%2 == 0 && $dateSeparated[1] > 0 && $dateSeparated[1] <= 7)||($dateSeparated[1]%2 != 0 && $dateSeparated[1] > 7 && $dateSeparated[1] <=12)){
                        //cek apakah bulan tersebut April, Juni, September, November
                        if($dateSeparated[2] > 0 && $dateSeparated[2] <= 30){
                            $checkDate = true;
                        }else{
                            $checkDate = false;
                        }
                    }else{
                            $checkDate = false;
                    }
                }
            }
            
            if($checkUsername && $checkEmail && $checkDate){
                //if(isset($_FILES["avatar"]["type"])){
                //check image file type
                    if ($_FILES["avatar"]["type"] != "image/jpeg"){
                        echo "File belum terisi, tipe File seharusnya jpg/jpeg";
                    }
                    else {                        
                        if(is_uploaded_file($_FILES['avatar']['tmp_name'])){
                            $fileNameImage = $username.".jpg"; //get the file name
                            $move = move_uploaded_file($_FILES['avatar']['tmp_name'], 'C://xampp/htdocs/rergag/images/avatar/'.$fileNameImage); //save image to the folder
                            if ($move){echo "Posting berhasil!";}
                        }
                    }
                //}
                if(isset($fileNameImage) && isset($about)){
                    $queryInsertNewUser = "INSERT INTO user VALUES ('".$username."','".$password."','".$email."','".$about."','".$gender."','images/avatar/".$fileNameImage."','".$birthdate."')";
                }
                else{
                    if(!isset($fileNameImage) && isset($about)){
                        $queryInsertNewUser = "INSERT INTO user VALUES ('".$username."','".$password."','".$email."','".$about."','".$gender."',null,'".$birthdate."')";
                    }else{
                        if(isset($fileNameImage) && !isset($about)){
                            $queryInsertNewUser = "INSERT INTO user VALUES ('".$username."','".$password."','".$email."',null,'".$gender."','images/avatar/".$fileNameImage."','".$birthdate."')";
                        }else{
                            $queryInsertNewUser = "INSERT INTO user VALUES ('".$username."','".$password."','".$email."',null,'".$gender."',null,'".$birthdate."')";
                        }
                    }
                }
                mysql_query($queryInsertNewUser) or die ("GAGAL MELAKUKAN QUERY. " . mysql_error());
                echo "Registration Success";
                $_SESSION['user_name']= $username;
                $_SESSION['login']= true;

                echo "<script>document.location.href='profile.php'</script>";

            }else{
                //$_SESSION['id_content']=$curIDContent;
                if(!isset($username) || !isset($email) || !isset($birthdate)){
                    
                }else{
                    echo "<script>document.location.href='register.php'</script>";
                }
                if(!$checkUsername){
                    echo "Username already exists";
                }
                if(!$checkEmail){
                    echo "Email was already used";
                }
                if(!$checkDate){
                    echo "Date is not valid";
                }
            }

        ?>
        </div>
        <script type="text/javascript" src="script/progin.js"></script>
        </div>
    </body>
</html>
<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
